const util = require('../../../../../utils/util.js');
const api = require('../../../../../config/api.js');
const app = getApp();
Page({
  data: {
    journeyImageList:[],//图片数组
    openSelectCover: false,//选择封面面板
    coverIndex: 0,//设置的封面
    selectCoverIndex: null,//选择的封面
    openDelImg: false,//删除面板
    selectAllLable: [],//选择的类目标签
    selectLableTextArray: [],// 选中的类目标签文字版
    openInput: false,//是否开启输入
    intro:'',//文字介绍
    addr: '',//地址简称
    addrLatitude: '',//地址纬度
    addrLongitude: '',//地址经度
    address: '',//地址全称
    privacy: false,//隐私状态
    openUploadPop: false,//上传路径面板
  },
  onLoad: function (options) {
    var _this = this;
    _this.setData({
      logininfo: app.globalData.logininfo,
      openid: app.globalData.logininfo.openid,
      journeyImageList: app.journey.imageList,
      coverIndex: app.journey.coverIndex,
      selectAllLable: app.journey.selectAllLable,
      selectLableTextArray: app.journey.selectLableTextArray,
      intro: app.journey.intro,
      addr: app.journey.addr,
      addrLatitude: app.journey.addrLatitude,
      addrLongitude: app.journey.addrLongitude,
      address: app.journey.address,
      privacy: app.journey.privacy,
    });
  },
  //打开选择封面面板
  showSelectCover: function () {
    var _this = this
    this.setData({
      openSelectCover: true,
    })
  },
  //选择封面图片
  selectCover: function(e){
    var _this = this
    var index = e.currentTarget.dataset.index
    _this.setData({
      selectCoverIndex: index
    })
  },
  //确定选择封面图片
  submitSelectCover:function(){
    var _this = this
    var index = _this.data.selectCoverIndex
    _this.setData({
      coverIndex: index
    })
    app.journey.coverIndex = index
    _this.closeMC();
  },
  //打开删除图片面板
  showDelImg:function(e){
    var _this = this
    var index = e.currentTarget.dataset.index
    this.setData({
      openDelImg: true,
      delIndex: index
    })
  },
  //删除图片 
  deleteImg: function(){
    var _this = this
    var index = _this.data.delIndex
    var arr = _this.data.journeyImageList
    arr.splice(index, 1)
    this.setData({
      openDelImg: false,
      journeyImageList: arr
    })
    app.journey.imageList = arr
  },
  //打开输入
  showInput: function () {
    var _this = this
    this.setData({
      openInput: true,
    })
  },
  //关闭输入
  closeInput: function () {
    var _this = this
    this.setData({
      openInput: false,
    })
  },
  //获取输入的介绍
  inputIntro: function (e) {
    var _this = this
    var value = e.detail.value.replace(/\s+/g, '')
    _this.setData({
      intro: value,
    })
    app.journey.intro = value
  },
  //打开选择地址组件
  openAddrPicker: function () {
    var _this = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              _this.albumInfoPositionStart();
            },
            fail: function () {
            }
          })
        } else {
          _this.albumInfoPositionStart();
        }
      }
    })
  },
  albumInfoPositionStart: function () {
    var _this = this;
    wx.chooseLocation({
      success: function (res) {
        var value = res.name;
        var latitude = res.latitude;
        var longitude = res.longitude;
        var address = res.address;
        if (value != null && value.length > 0) {
          _this.setData({
            addr: value,
            addrLatitude: latitude,
            addrLongitude: longitude,
            address: address,
          })
          app.journey.addr = value
          app.journey.addrLatitude = latitude
          app.journey.addrLongitude = longitude
          app.journey.address = address
        } else {
          util.dialogAlert("未选中有效地址")
        }
      }
    })
  },
  //隐私开关
  changePrivacy: function (e) {
    var _this = this
    _this.setData({
      privacy: e.detail.value
    })
    app.journey.privacy = e.detail.value
  }, 
  //打开选择上传路径面板
  showUploadPop: function () {
    var _this = this
    var str = [{ "title": "拍照", "type": "img" }, { "title": "从手机相册选择", "type": "img" }, { "title": "从微信导入图片", "type": "img" }]
    _this.setData({
      popText: str
    })
    this.setData({
      openUploadPop: true,
    })
  },
  //打开拍摄
  openShoot: function () {
    var _this = this
    var imgArr = []
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['camera'],
      success(res) {
        _this.closeMC();
        wx.redirectTo({
          url: '/ucenter/pages/ucenter/personinfo/avatar/index?atype=journeyImg&imageSrc=' + res.tempFilePaths[0],
        })
      }
    })
  },
  //打开相册
  openAlbum: function () {
    var _this = this
    var imgArr = []
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success(res) {
        _this.closeMC();
        wx.redirectTo({
          url: '/ucenter/pages/ucenter/personinfo/avatar/index?atype=journeyImg&imageSrc=' + res.tempFilePaths[0],
        })
      }
    })
  },
  //打开微信文件
  openWeChatFile: function () {
    var _this = this
    var imgArr = []
    wx.chooseMessageFile({
      count: 1,
      type: 'image',
      success(res) {
        console.log(res)
        _this.closeMC();
        wx.redirectTo({
          url: '/ucenter/pages/ucenter/personinfo/avatar/index?atype=journeyImg&imageSrc=' + res.tempFiles[0].path,
        })
      }
    })
  },
  //保存草稿
  save: function(){
    var _this = this
    var imgList = _this.data.journeyImageList
    var lableList = _this.data.selectAllLable
    if (imgList != '' && imgList != null && lableList != '' && lableList!=null){
      wx.redirectTo({
        url: '/grade/pages/grade/journey/journey',
      })
      app.journey.imageList = []
      app.journey.coverIndex = 0
      app.journey.selectAllLable = []
      app.journey.selectLableTextArray = []
      app.journey.intro = ''
      app.journey.addr = ''
      app.journey.addrLatitude = ''
      app.journey.addrLongitude = ''
      app.journey.address = ''
      app.journey.privacy = false
    }else{
      if (imgList == '' || imgList == null){
        wx.showToast({
          title: '请先上传图片',
          icon: 'none',
          duration: 1500
        })
      }else{
        if (lableList == '' || lableList == null) {
          wx.showToast({
            title: '请选择类目',
            icon: 'none',
            duration: 1500
          })
        }
      }
    }
  },
  //发布
  publish: function(){
    var _this = this
    var imgList = _this.data.journeyImageList
    var lableList = _this.data.selectAllLable
    if (imgList != '' && imgList != null && lableList != '' && lableList != null) {
      wx.redirectTo({
        url: '/grade/pages/grade/journey/journey',
      })
      app.journey.imageList = []
      app.journey.coverIndex = 0
      app.journey.selectAllLable = []
      app.journey.selectLableTextArray = []
      app.journey.intro = ''
      app.journey.addr = ''
      app.journey.addrLatitude = ''
      app.journey.addrLongitude = ''
      app.journey.address = ''
      app.journey.privacy = false
    } else {
      if (imgList == '' || imgList == null) {
        wx.showToast({
          title: '请先上传图片',
          icon: 'none',
          duration: 1500
        })
      } else {
        if (lableList == '' || lableList == null) {
          wx.showToast({
            title: '请选择类目',
            icon: 'none',
            duration: 1500
          })
        }
      }
    }
  },
  //跳转编辑图片
  toMake: function(e){
    var _this = this
    var index = e.currentTarget.dataset.index
    var info = _this.data.journeyImageList[index]
    console.log(info)
    wx.redirectTo({
      url: '../makeImage?type=again&index='+index+'&info='+JSON.stringify(info),
    })
  },
  //跳转选择类目
  gotoSlectLable: function(){
    var _this = this
    wx.navigateTo({
      url: '/ucenter/pages/ucenter/personcenter/publish/itemlabel/itemlabel?type=journeyImg&selectAllLable=' + JSON.stringify(_this.data.selectAllLable)
    })
  },
  //关闭面板
  closeMC: function () {
    var _this = this
    _this.setData({
      openUploadPop: false,
      openSelectCover: false,
      openDelImg: false,
    })
  },
})